package cliente;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;

public class URLReaderPlayers extends JFrame{
	
	static URL pagina;
	static BufferedReader in;
	static String entradaOriginal, entrada, entrada2, entrada3, entrada4, entrada5, entrada6, entrada7, entrada8, entrada9, entrada10, entrada11, entrada12, entrada13, entrada14, entrada15, entrada16, entrada17, entrada18; 
	static String position, level, name, rating, nationpicture, picture, clubpicture, revision, rare, playerid, contarJugadores, jugadorId; 
	static String tipoRare, tipoLevel,tiporating, tiponame, tipoposition, tipoplayerid, convertido;
	static String [] tarjeta, bandera, jugador, club, card;
	static String [] jugadorEncontrado, posicionEncontrado, ratingEncontrado, pacEncontrado, shoEncontrado, pasEncontrado, driEncontrado, defEncontrado, heaEncontrado;  
	static	int lead, contador, fila;
	static Image imagecarta, imagejugador, imageequipo, imagenation;	
	static URL carta, jugadorface, equipo, nation;
	static JLabel label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12, label13;
	static JTextArea textArea;
	static JPanel panelImagenPlayers = new JPanel();
	static ListSelectionModel selectionModel;
	static Font font18 = new Font("Helvetica", Font.BOLD, 18);
	static Font font42 = new Font("Helvetica", Font.BOLD, 42);
	
	static MouseAdapter adaptador = new MouseAdapter() { public void mouseClicked(MouseEvent evt) {
		int row = GuiCliente.tablaBuscar.rowAtPoint(evt.getPoint());
		if (row >= 0) {
			switch(GuiCliente.seleccionado){
			case "Players":
				panelImagenPlayer(row);
				getLead(row);
				break;
			case "Contracts":
				URLReaderContracs.panelImagenContract(row);
				getLead(row);
				break;
//			case "Fitness":
//				pulsadoBuscarFitness();
//				break;
//			case "Formations":
//				pulsadoBuscarFormations();
//				break;
			}
		}	
	}};
	
	public URLReaderPlayers(){
		rating="rating";level="level";name="name";position="position";revision="revision";rare="rare";playerid="\"playerid";
		nationpicture="nationpicture";picture="\"picture";clubpicture="clubpicture";
		tiporating="tiporating";tiponame="tiponame";tipoposition="tipoposition";tipoLevel="tipoLevel";tipoRare="tipoRare";tipoplayerid="tipoplayerid";		
		jugadorEncontrado = new String[1000];posicionEncontrado = new String[1000];ratingEncontrado = new String[1000];pacEncontrado = new String[1000];
		shoEncontrado = new String[1000];pasEncontrado = new String[1000];driEncontrado = new String[1000];defEncontrado = new String[1000];heaEncontrado = new String[1000];
		bandera = new String[1000];jugador = new String[1000];club = new String[1000];card = new String[1000];tarjeta = new String[1000];
	}
	
	static void marcarFila(){
		GuiCliente.tablaBuscar.addMouseListener(adaptador);
	}
	
	static void seleccionarJugadores (String playero){
		System.out.println("Buscando jugador");
		try {
			pagina = new URL("http://www.futhead.com/fifa/players/search/?term="+playero);
			in = new BufferedReader (new InputStreamReader(pagina.openStream()));
			entradaOriginal = in.readLine();
			contarJugadores =entradaOriginal;
			entrada=entradaOriginal;
			System.out.println("control:"+entradaOriginal);

		}catch (IOException e) {e.printStackTrace();}
		
		contador=GuiCliente.modeloTablaBuscar.getRowCount();

		if(contador!=0){
			for(int x=0;x<contador;x++){
				GuiCliente.modeloTablaBuscar.removeRow(0);
			}
			contador=0;
		}
		
	    while (contarJugadores.indexOf(rating) > -1) {
	    	contarJugadores = contarJugadores.substring(contarJugadores.indexOf(rating)+rating.length(),contarJugadores.length());
		    contador++; 
		}

	    for (int x=0;x<contador;x++) {
	    	entrada = entrada.substring(entrada.indexOf(rating)+rating.length(),entrada.length());
	    	entrada2 = entrada.substring(entrada.indexOf(level)+level.length(),entrada.length());
	    	entrada3 = entrada.substring(entrada.indexOf(name)+name.length(),entrada.length());
	    	entrada4 = entrada.substring(entrada.indexOf(position)+position.length(),entrada.length());
	    	entrada5 = entrada.substring(entrada.indexOf(revision)+revision.length(),entrada.length());
	    	entrada6 = entrada.substring(entrada.indexOf(rare)+rare.length(),entrada.length());
	    	entrada7 = entrada.substring(entrada.indexOf(nationpicture)+nationpicture.length(),entrada.length());
	    	entrada8 = entrada.substring(entrada.indexOf(picture)+picture.length(),entrada.length());
	    	entrada9 = entrada.substring(entrada.indexOf(clubpicture)+clubpicture.length(),entrada.length());
	    	
	    	entrada10 = entrada.substring(entrada.indexOf("attr1")+"attr1".length(),entrada.length());
	    	entrada11 = entrada.substring(entrada.indexOf("attr2")+"attr2".length(),entrada.length());
	    	entrada12 = entrada.substring(entrada.indexOf("attr3")+"attr3".length(),entrada.length());
	    	entrada13 = entrada.substring(entrada.indexOf("attr4")+"attr4".length(),entrada.length());
	    	entrada14 = entrada.substring(entrada.indexOf("attr5")+"attr5".length(),entrada.length());
	    	entrada15 = entrada.substring(entrada.indexOf("attr6")+"attr6".length(),entrada.length());
	    	
	    	entrada16= entrada.substring(entrada.indexOf(playerid)+playerid.length(),entrada.length());
	    	
	    	tiporating=entrada.substring(entrada.indexOf(":")+2 , entrada.indexOf(","));
	    	tipoLevel=entrada2.substring(entrada2.indexOf(":")+3 , entrada2.indexOf(",")-1);
	    	tiponame=entrada3.substring(entrada3.indexOf(":")+3 , entrada3.indexOf(",")-1);
	    	tipoposition=entrada4.substring(entrada4.indexOf(":")+3 , entrada4.indexOf("}")-1);
	    	tipoRare=entrada6.substring(entrada6.indexOf(":")+2 , entrada6.indexOf(","));
	    	
	    	tipoplayerid=entrada16.substring(entrada16.indexOf(":")+2 , entrada16.indexOf(","));

	    	convertido = convertirUnicode(tiponame);
	    	
		    pacEncontrado[x]=entrada10.substring(entrada10.indexOf(":")+3 , entrada10.indexOf(",")-1);
			shoEncontrado[x]=entrada11.substring(entrada11.indexOf(":")+3 , entrada11.indexOf(",")-1);
			pasEncontrado[x]=entrada12.substring(entrada12.indexOf(":")+3 , entrada12.indexOf(",")-1);
			driEncontrado[x]=entrada13.substring(entrada13.indexOf(":")+3 , entrada13.indexOf(",")-1);
			defEncontrado[x]=entrada14.substring(entrada14.indexOf(":")+3 , entrada14.indexOf(",")-1);
			heaEncontrado[x]=entrada15.substring(entrada15.indexOf(":")+3 , entrada15.indexOf(",")-1);
			jugadorEncontrado[x]=convertido;
			posicionEncontrado[x]=tipoposition;
			ratingEncontrado[x]=tiporating;
			
			
	    	bandera[x]=entrada7.substring(entrada7.indexOf(":")+3 , entrada7.indexOf(",")-1);
	    	jugador[x]=entrada8.substring(entrada8.indexOf(":")+3 , entrada8.indexOf(",")-1);
	    	club[x]=entrada9.substring(entrada9.indexOf(":")+3 , entrada9.indexOf(",")-1);
	    	card[x]=entrada5.substring(entrada5.indexOf(":")+3 , entrada5.indexOf(",")-1);
	    	
	    	switch(card[x]){
	    	case "Upgrade":
	    		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-rare.png";
	    		}
	    		else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-rare.png";
		    	}
	    		else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-rare.png";
		    	}
		   		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-nonrare.png";
		   		}
		   		else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-nonrare.png";
		    	}
		   		else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-nonrare.png";
		    	}
	    		break;
	    	case "on":
	    		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-rare.png";
	    		}
		   		else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-rare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-rare.png";
			   	}
		    	if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-nonrare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-nonrare.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-nonrare.png";
			    }
	    		break;
	    	case "MOTM":
	    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-orange.png";
	    		break;
	    	case "TOTS":
	    		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-gold.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-silver.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-bronze.png";
			    }
	    		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("false")==true){
	    			tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-gold.png";
			    }
			    else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("false")==true){
			    	tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-silver.png";
				}
			    else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("false")==true){
			    	tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-bronze.png";
				}
	    		break;
	    	case "Transfer":
	    		if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-rare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-rare.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-rare.png";
			    }
		    	if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-nonrare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-nonrare.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-nonrare.png";
			    }
	    		break;
	    	case "TOTYR":
	    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue-red.png";
	    		break;
	    	case "":
		    	if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-rare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-rare.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("true")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-rare.png";
			    }
		    	if(tipoLevel.equalsIgnoreCase("gold")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-gold-nonrare.png";
		    	}
		    	else if(tipoLevel.equalsIgnoreCase("silver")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-silver-nonrare.png";
			    }
		    	else if(tipoLevel.equalsIgnoreCase("bronze")==true && tipoRare.equalsIgnoreCase("false")==true){
		    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-bronze-nonrare.png";
			    }
	    		break;
	    	case "IF":
	    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-if-gold.png";
	    		break;
	    	case "TOTY":
	    		tarjeta[x]="http://fh13.fhcdn.com/static/img/cards/card200_player-blue.png";
	    		break;
	    	}
	    	Object nuevaFila[]= {tipoplayerid,tiporating,tipoLevel,convertido,tipoposition};
	    	GuiCliente.modeloTablaBuscar.addRow(nuevaFila);
	    }
	    try {
	    	
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    getplayerId(tipoplayerid);
	}
	
	static void panelImagenPlayer(int lead){	
		System.out.println("panel imagen");
		System.out.println("Selecciona la fila "+lead);
		panelImagenPlayers.setLayout(null);
		panelImagenPlayers.removeAll();
		try {
		carta = new URL(tarjeta[lead]);imagecarta = ImageIO.read(carta);ImageIcon icono1=new ImageIcon(imagecarta);
        jugadorface = new URL(jugador[lead]);imagejugador = ImageIO.read(jugadorface);ImageIcon icono2=new ImageIcon(imagejugador);
        equipo = new URL(club[lead]);imageequipo = ImageIO.read(equipo);ImageIcon icono3=new ImageIcon(imageequipo);
        ImageIcon iconor3 = new ImageIcon(icono3.getImage().getScaledInstance(50,50,Image.SCALE_SMOOTH));
        nation = new URL(bandera[lead]);imagenation = ImageIO.read(nation);ImageIcon icono4=new ImageIcon(imagenation);
        label1 = new JLabel(icono4);label2 = new JLabel(icono2);label3 = new JLabel(iconor3);label4 = new JLabel(icono1);	
        
        label5 = new JLabel(pacEncontrado[lead]);label6 = new JLabel(shoEncontrado[lead]);label7 = new JLabel(pasEncontrado[lead]);label8 = new JLabel(driEncontrado[lead]);
        label9 = new JLabel(defEncontrado[lead]);label10 = new JLabel(heaEncontrado[lead]);label12 = new JLabel(posicionEncontrado[lead]);
        label13 = new JLabel(ratingEncontrado[lead]);textArea = new JTextArea(jugadorEncontrado[lead]);
        
		}catch (MalformedURLException e){e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
		URLReaderContracs.panelImagenContract.setVisible(false);
		panelImagenPlayers.setVisible(false);
		panelImagenPlayers.setBounds(535, 20, 210, 280);GuiCliente.panelSearchPlayers.add(panelImagenPlayers);panelImagenPlayers.setVisible(true);

        label5.setBounds(20,177,100,50);panelImagenPlayers.add(label5);label5.setFont(font18);label5.setVisible(true);label5.setForeground(Color.white);//PAC
        label6.setBounds(20,205,100,50);panelImagenPlayers.add(label6);label6.setFont(font18);label6.setVisible(true);label6.setForeground(Color.white);//SHO
        label7.setBounds(20,232,100,50);panelImagenPlayers.add(label7);label7.setFont(font18);label7.setVisible(true);label7.setForeground(Color.white);//PAS
        label8.setBounds(120,177,100,50);panelImagenPlayers.add(label8);label8.setFont(font18);label8.setVisible(true);label8.setForeground(Color.white);//DRI
        label9.setBounds(120,205,100,50);panelImagenPlayers.add(label9);label9.setFont(font18);label9.setVisible(true);label9.setForeground(Color.white);//DEF
        label10.setBounds(120,232,100,50);panelImagenPlayers.add(label10);label10.setFont(font18);label10.setVisible(true);label10.setForeground(Color.white);//HEA
        textArea.setBounds(105,20,105,100);panelImagenPlayers.add(textArea);textArea.setFont(font18);textArea.setVisible(true);textArea.setForeground(Color.white);//Name
        textArea.setLineWrap(true);textArea.setWrapStyleWord(true);textArea.setOpaque(false);textArea.setEditable(false);
        label12.setBounds(35,45,60,37);panelImagenPlayers.add(label12);label12.setFont(font18);label12.setVisible(true);label12.setForeground(Color.white);//Posicion
        label13.setBounds(25,15,60,37);panelImagenPlayers.add(label13);label13.setFont(font42);label13.setVisible(true);label13.setForeground(Color.white);//Rating
		
        label1.setBounds(20,140,60,37);panelImagenPlayers.add(label1);label1.setVisible(true);
        label2.setBounds(95,80,100,100);panelImagenPlayers.add(label2);label2.setVisible(true);
        label3.setBounds(25,80,50,50);panelImagenPlayers.add(label3);label3.setVisible(true);
        label4.setBounds(0, 0, 210, 280);panelImagenPlayers.add(label4);label4.setVisible(true);	
       
	}

	static void getplayerId(String tipoplayerid){
		jugadorId=tipoplayerid;
	}
	
	static String setplayerId(){
		return jugadorId;
	}
	
	static void getLead(int row){
		fila=row;
	}
	
	static int setLead(){
		return fila;
	}
	
	static String convertirUnicode(String texto){
		String retorno="";
		int hexVal;
		String [] unicode, palabras, ultima, primera;
		int cont=0;
		String letra1=texto;
		String textoOriginal=texto;
		boolean palabrainicial=false;
		
		while (textoOriginal.indexOf("\\") > -1) {
			textoOriginal = textoOriginal.substring(textoOriginal.indexOf("\\")+"\\".length(),textoOriginal.length());
			cont++;
		}
		
		unicode = new String[cont];
		ultima = new String[100];
		palabras = new String[100];
		primera = new String[100];
		
		for(int x=0;x<cont;x++){

			if(x==0&&letra1.substring(letra1.indexOf(""), letra1.indexOf("u")).equals("\\")==false){
				primera[x]=texto.substring(texto.indexOf("") , texto.indexOf("\\"));
				palabras[x]="";
				ultima[x]="";
				palabrainicial=true;
			}
			
			texto = texto.substring(texto.indexOf("\\")+"\\".length(),texto.length());
			unicode[x]=texto.substring(texto.indexOf("u")+1 , texto.indexOf("u")+5);

			if(texto.indexOf("\\") > -1){
				if(palabrainicial==true){
				palabras[x]=texto.substring(texto.indexOf("u")+5 , texto.indexOf("\\"));	
				ultima[x]="";
				palabrainicial=false;
				
				}else{
					primera[x]="";
					palabras[x]=texto.substring(texto.indexOf("u")+5 , texto.indexOf("\\"));
					ultima[x]="";
				}
			}else{
				if(texto.length()>5){
					if(palabrainicial==true){
						palabras[x]="";
						ultima[x]=texto.substring(texto.indexOf("")+5 , texto.length());
					}
					else{
						primera[x]="";
						palabras[x]="";
						ultima[x]=texto.substring(texto.indexOf("")+5 , texto.length());
					}
				}else{
					if(palabrainicial==true){
						palabras[x]="";
						ultima[x]="";}
					else{
						primera[x]="";
						palabras[x]="";
						ultima[x]="";
					}
				}
			}
		}

		if(cont != 0){
		for(int y=0;y<cont;y++){
			hexVal = Integer.parseInt(unicode[y], 16);
			retorno += primera[y]+(char)hexVal+palabras[y]+ultima[y];	
		}
		}else{
			return texto;
		}
		return retorno;
	}
}